﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Smart_Archives.Entity;
using Smart_Archives.Utils;

namespace Smart_Archives.Controllers
{
    /// <summary>
    /// 用户登录控制器
    /// </summary>
    public class LoginController : Controller
    {
        private readonly SaContext _saContext;

        public LoginController(SaContext saContext)
        {
            _saContext = saContext;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 登录请求处理
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            var username = Request.Form["username"];
            var pwd = Request.Form["pwd"];
            var user = _saContext.User.FirstOrDefault(a =>
                a.Username == username && a.Password == Md5Util.Md5Hash(Md5Util.Md5Hash(pwd)));
            return Json(user == null ? new { succeed = false, msg = "登陆失败" } : new { succeed = true, msg = "登陆失败" });
        }
    }
}